/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */
package seedpod.model.rdb;

/**
 * <p>Title: KnowldgeBase to Database</p>
 * <p>Description: Transform Protege Knowledgebase RDB schema</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: University of Washington BHI</p>
 * @author Hao Li
 * @version 2.0
 */

import java.util.Vector;

import seedpod.util.XMLWriter;


public class View extends Relation
{
    private Vector<Relation> _unionTables = new Vector<Relation>(); // <Relation>:aggregate of
    // relations
    private Relation _concreteTable = null; // view's concrete class table,
    // default is abstract class

    public View(String name) 
    {
        super( name );
    }

    public View(Rdb rdb, String name) {
        super( rdb, name );
    }
    

    public void setConcreteClassTable( Relation table ) {
        _concreteTable = table;
        if ( table.getPrimaryKey() != null )
        	 setPrimaryKey( table.getPrimaryKey() );
        addToUnionRelation( table );
    }
    
    public Attribute getPrimaryKey()  {
    	Attribute pk = super.getPrimaryKey();
    	try {
			pk = getAttribute( Relation.DEFAULT_PRIMARY_KEY );
		}
		catch (NoAttributeFoundException e) {
			pk = _rdb.addAttribute( this, Relation.DEFAULT_PRIMARY_KEY, RdbValueType.INTEGER, true );
			System.err.println( "add primary key for " + getName() );
			// e.printStackTrace();
		}
    	return pk;
    }
    
    public Relation getConcreteClassTable() {
        return _concreteTable;
    }

    public void addToUnionRelation( Relation table ) {
        if ( !_unionTables.contains( table ) )
            _unionTables.add( table );
    }

    public Vector<Relation> getUnionRelations() {
        return _unionTables;
    }
    
   

    /**
     * @todo implement view definition such that what it select aggregates from
     *       is defined.
     * @param out
     * @return
     * @throws XMLWriter.XMLException
     */
    public XMLWriter serializeXML( XMLWriter out )
            throws XMLWriter.XMLException {
        XMLWriter viewStream = out.addElement( "view" );
        viewStream.addElement( "name", _name );

        // attributes
        Attribute att;
        Vector<Attribute> attributes = getAttributes();
        for ( int i = 0; i < attributes.size(); i++ ) {
            att = (Attribute)attributes.elementAt( i );
            att.serializeXML( viewStream );
        }
        return viewStream;
    }
}